package com.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.wx.bean.GroupBean;
import com.wx.bean.PointBean;
import com.wx.bean.Userbean;
import com.wx.service.impl.IPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/point")
public class PointController {
    @Autowired
    IPointService iPointService;

    @RequestMapping("/findByName")
    public JSONObject findByName(@RequestBody Userbean userbean, HttpSession session){
        String name= (String) session.getAttribute("username");
        userbean.setName(name);
        Userbean userbean1=iPointService.findByName(userbean);
        userbean.setGroupCode(userbean1.getGroupCode());
        List<Userbean> list=iPointService.findByCode(userbean);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        return jsonObject;
    }

    @RequestMapping("/pointAdd")
    public JSONObject pointAdd(@RequestBody List<PointBean> list){
        Date d = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String data=sdf.format(d);
        for (PointBean pointBean:list) {
            pointBean.setTime(data);
        }
        int result=iPointService.pointAdd(list);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }

//全部查询，未使用
    @RequestMapping("/pointTableFandAll")
    public JSONObject pointTableFandAll(){
        List<PointBean> list=iPointService.pointTableFandAll();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        return jsonObject;
    }

    @RequestMapping("/findGroupName")
    public JSONObject findGroupName(){
        List<GroupBean> list=iPointService.findGroupName();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        return jsonObject;
    }

    @RequestMapping("/findTeamLeaderLame")
    public JSONObject findTeamLeaderLame(HttpSession session){
        String teamLeaderLame= (String) session.getAttribute("username");
        int result=iPointService.findTeamLeaderLame(teamLeaderLame);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }

    @RequestMapping("/conditionFind")
    public JSONObject conditionFind(@RequestBody PointBean pointBean){
        List<PointBean> list=iPointService.conditionFind(pointBean);
        int number=iPointService.findNumber(pointBean);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("number",number);
        return jsonObject;
    }


    @RequestMapping("/dateDelete")
    public JSONObject dateDelete(@RequestBody PointBean pointBean){
        int result=iPointService.dateDelete(pointBean);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }


    @RequestMapping("/findTime")
    public JSONObject findTime(@RequestBody PointBean pointBean){
        Date d = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String data=sdf.format(d);
        pointBean.setTime(data);
        int number=iPointService.findTime(pointBean);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("number",number);
        return jsonObject;
    }
}
